﻿//********************************************************************************//
//创建日期：<创建日期，2016-03-31>
//创建作者：<郭登杰，guodengjie@shanghai3h.com>
//功能说明：处理账单相关
//********************************************************************************//
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Entity;
using BLL;
using BLL.Access;
using YXYLCommon;
using System.Web;
using System.Web.SessionState;

namespace Web.ashx.QSSFZW
{
    public class ZhangDan : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int? rsno = -1;
            string rsmsg = "";
            if (context.Request["op"] == "ZDXX")
            {
                int type = int.Parse(context.Request["type"]);
                string yonghuh = context.Request["yhh"];
                int y1 = int.Parse(context.Request["y1"]);
                int y2 = int.Parse(context.Request["y2"]);
                List<QSSFZhangDanXX> list = new DALQSSFZhangDanXX().Get(type, yonghuh, y1, y2);
                LayerJsonRs jrs = new LayerJsonRs(list.Count,list);
                context.Response.Write(JsonConvert.SerializeObject(jrs));
            }
            else if (context.Request["op"] == "ZDXX_DBS_HQ")
            {
                int ym = int.Parse(context.Request["ym"]);
                int printed = int.Parse(context.Request["printed"]);
                DbDateTime printdate = DbDateTime.Parse(context.Request["printdate"]);
                List<QSSFZhangDanXX_DBS> list = new DALQSSFZhangDanXX_DBS().Get(ym, printed, printdate);
                context.Response.Write(JsonConvert.SerializeObject(list));
            }
            else if (context.Request["op"] == "ZDXX_DBS_HQ2")
            {
                int zt = int.Parse(context.Request["zt"]);
                int ym = int.Parse(context.Request["ym"]);
                string hm = context.Request["hm"];
                DbDateTime printdate = DbDateTime.Parse(context.Request["printdate"]);
                List<QSSFZhangDanXX_DBS> list = new DALQSSFZhangDanXX_DBS().Get2(zt,ym, hm, printdate);
                context.Response.Write(JsonConvert.SerializeObject(list));
            }
            else if (context.Request["op"] == "DXS_YDLIST")
            {
                int zt = int.Parse(context.Request["zt"]);
                int ym = int.Parse(context.Request["ym"]);
                string hm = context.Request["hm"];
                DbDateTime printdate = DbDateTime.Parse(context.Request["printdate"]);
                List<QSSFZhangDanXX> list = new DALQSSFZhangDanXX().Get2(zt, ym, hm, printdate);
                LayerJsonRs jrs = new LayerJsonRs(list.Count,list);
                context.Response.Write(JsonConvert.SerializeObject(jrs));
            }
            else if (context.Request["op"] == "DXS_DYZTBG")//已打发票变更打印状态 成功与打坏的切换
            {
                //获取打印状态,账单编号,发票号码,用户号,用户名,开户户名
                int dyzt = int.Parse(context.Request["dyzt"]);
                string fphms = context.Request["fphms"];
                DbCmdResult dbrs = new DALQSSFZhangDanXX().DaYinZTBG(dyzt, fphms);
                context.Response.Write(JsonConvert.SerializeObject(new JsonRs(dbrs.IsSuccess, dbrs.ReturnMsg, dbrs.ReturnMsg.Split('|')[0])));
            }
            else if (context.Request["op"] == "DXS_DYTJ")
            {
                DbDateTime printdate = DbDateTime.Parse(context.Request["printdate"]);
                List<QSSFZhangDanDYTJ> list = new DALQSSFZhangDanXX().DaYinTJGet(printdate);
                context.Response.Write(JsonConvert.SerializeObject(list));
            }
            else if (context.Request["op"] == "DY")
            {
                //获取打印状态,账单编号,发票号码,用户号,用户名,开户户名
                string dyzt = context.Request["dyzt"];
                string zdbhs = context.Request["zdbhs"];
                string fphms = context.Request["fphms"];
                string yhhs = context.Request["yhhs"];
                string yhms = context.Request["yhms"];
                string khhms = context.Request["khhms"];
                int sffs = int.Parse(context.Request["sffs"]);

                //成功:1 打坏:2
                new SpHelper().P_QSSF_JJZD_DY(dyzt == "success" ? 1 : 2, zdbhs, fphms, yhhs, yhms, khhms,sffs, DateTime.Now, LoginInfo.CurrentUser.UserID.ToString(), out rsno, out rsmsg);
                JsonRs rs = new JsonRs(rsno == 1, rsmsg, null);
                context.Response.Write(JsonConvert.SerializeObject(rs));
            }
            else if (context.Request["op"] == "DY_DBS")
            {
                //获取打印状态,账单编号,发票号码,用户号,用户名,开户户名
                int dyzt = int.Parse(context.Request["dyzt"]);
                string zdbh = context.Request["zdbh"];
                string fphm = context.Request["fphm"];
                DateTime printdate = DateTime.Parse(context.Request["printdate"]);
                string returndate = context.Request["returndate"];
                string txm = "";
                //成功:1 打坏:2
                new SpHelper().P_QSSF_ZD_DY_DBS(dyzt, BusinessTool.ZHANDIANBH() == "00" ? "51" : BusinessTool.ZHANDIANBH(), zdbh, fphm, printdate, returndate, out txm, out rsno, out rsmsg);
                JsonRs rs = new JsonRs(rsno == 1, rsmsg, txm);
                context.Response.Write(JsonConvert.SerializeObject(rs));
            }//DXS_DYZTBG
            else if (context.Request["op"] == "DYZTBG_DBS")//已打发票变更打印状态 成功与打坏的切换
            {
                //获取打印状态,账单编号,发票号码,用户号,用户名,开户户名
                int dyzt = int.Parse(context.Request["dyzt"]);
                string fphms = context.Request["fphms"];
                DbCmdResult dbrs= new DALQSSFZhangDanXX_DBS().DaYinZTBG(dyzt, fphms);
                context.Response.Write(JsonConvert.SerializeObject(new JsonRs(dbrs.IsSuccess,dbrs.ReturnMsg,dbrs.ReturnMsg.Split('|')[0])));
            }
            else if (context.Request["op"] == "DYTJ_DBS")
            {
                DbDateTime printdate = DbDateTime.Parse(context.Request["printdate"]);
                List<QSSFZhangDanDYTJ> list = new DALQSSFZhangDanXX_DBS().DaYinTJGet(printdate);
                context.Response.Write(JsonConvert.SerializeObject(list));
            }
            else if (context.Request["op"] == "DXS_QFXX_HJ")
            {
                string yhh = context.Request["yhh"];
                DXSQianFeiXX obj = new DALDXSQFSFXX().GetQFHJ(yhh);
                JsonRs rs = new JsonRs();
                rs.Data1 = obj;
                rs.State=obj.IsExists;
                context.Response.Write(JsonConvert.SerializeObject(rs));
            }
            else if (context.Request["op"] == "DXS_QFXX_MX")
            {
                string yhh = context.Request["yhh"];
                context.Response.Write(JsonConvert.SerializeObject(new DALDXSQFSFXX().GetQFMX(yhh)));
            }
            else if (context.Request["op"] == "DXS_SF_ZPXJFW")
            {
                string zdbh = context.Request["zdbh"];
                decimal sfje=Convert.ToDecimal(context.Request["sfje"]);
                int zwym = int.Parse(context.Request["sfrq"]);
                int type = int.Parse(context.Request["type"]);
                DbCmdResult dbrs = new DALDXSQFSFXX().DBSF(zdbh, sfje, zwym, type, LoginInfo.CurrentUser.UserID.ToString());
                JsonRs rs = new JsonRs(dbrs.IsSuccess,dbrs.ReturnMsg,null);
                context.Response.Write(JsonConvert.SerializeObject(rs));
            }
            else if (context.Request["op"] == "DXS_SFXX_MX")
            {
                string zwym = context.Request["zwym"];
                string sfrq = context.Request["sfrq"];
                string yhh = context.Request["yhh"];
                List<DXSShouFeiXX> list = new DALDXSQFSFXX().GetSFMX(string.IsNullOrEmpty(zwym)?-1:int.Parse(zwym),string.IsNullOrEmpty(sfrq)?null:(DateTime?)Convert.ToDateTime(sfrq),yhh);
                context.Response.Write(JsonConvert.SerializeObject(list));
            }
            else if (context.Request["op"] == "DXS_SF_SFQX")
            {
                string ids=context.Request["ids"];
                string zdbhs = context.Request["zdbhs"];
                DbCmdResult dbrs = new DALDXSQFSFXX().SFQX(ids,zdbhs,LoginInfo.CurrentUser.UserID.ToString());
                JsonRs rs = new JsonRs(dbrs.IsSuccess, dbrs.ReturnMsg, null);
                context.Response.Write(JsonConvert.SerializeObject(rs));
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}